package com.example.demo.Service;


import com.example.demo.Convert.*;
import com.example.demo.Entity.CurrentStorage;
import com.example.demo.Entity.Item;
import com.example.demo.Entity.Shelf;
import com.example.demo.Entity.Warehouse;
import com.example.demo.Repository.CurrentStorageRepo;
import com.example.demo.Repository.WarehouseRepo;
import com.example.demo.Service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class WarehouseServiceImpl implements WarehouseService {

    private final WarehouseRepo warehouseRepository;
    private final CurrentStorageRepo currentStorageRepo;

    @Autowired
    public WarehouseServiceImpl(WarehouseRepo warehouseRepository,CurrentStorageRepo currentStorageRepo) {
        this.currentStorageRepo = currentStorageRepo;
        this.warehouseRepository = warehouseRepository;
    }

    @Override
    public Warehouse createWarehouse(Warehouse warehouse) {
        return warehouseRepository.save(warehouse);
    }

    @Override
    public Map<String, Object> searchWarehouseByName(String name) {
        Optional<Warehouse> warehouse = warehouseRepository.findByName(name);
        Map<String, Object> response = new HashMap<>();

        if(warehouse.isPresent()){
            response.put("exists",true);
            response.put("warehouse",warehouse.get());
            response.put("message","查找成功");

            System.out.println("查找成功，结果为："+warehouse.get());
        }else{
            response.put("exists",false);
            response.put("message","查找结果为空");
            System.out.println("查找结果为空");
        }
        return response;
    }

    public Map<String, Object> findAllWarehouse() {
        List<Warehouse> warehouse = warehouseRepository.findAll();
        Map<String, Object> response = new HashMap<>();

        if(!warehouse.isEmpty()){
            response.put("exists",true);
            response.put("warehouse",warehouse);
            response.put("message","查找成功");

            System.out.println("查找成功，结果为："+warehouse);
        }else{
            response.put("exists",false);
            response.put("message","查找结果为空");
            System.out.println("查找结果为空");
        }
        return response;
    }

    @Override
    public Optional<Warehouse> getWarehouseById(Long id) {
        return warehouseRepository.findById(id);
    }

    @Override
    public List<Warehouse> getAllWarehouses() {
        return warehouseRepository.findAll();
    }

    @Override
    public Warehouse updateWarehouse(Long id, Warehouse warehouse) {
        Optional<Warehouse> existingWarehouseOptional = warehouseRepository.findById(id);
        if (existingWarehouseOptional.isPresent()) {
            Warehouse existingWarehouse = existingWarehouseOptional.get();
            existingWarehouse.setName(warehouse.getName());
            existingWarehouse.setAddress(warehouse.getAddress());
            existingWarehouse.setCapacity(warehouse.getCapacity());
            existingWarehouse.setCurrentStorage(warehouse.getCurrentStorage());
            return warehouseRepository.save(existingWarehouse);
        } else {
            return null; // Or throw an exception
        }
    }

    @Override
    public boolean deleteWarehouse(Long id) {
        if (warehouseRepository.existsById(id)) {
            warehouseRepository.deleteById(id);
            return true;
        } else {
            return false; // Or throw an exception
        }
    }

    @Override
    public Map<String, Object> updateWarehouseByName(Warehouse warehouse) {
        Long id = warehouseRepository.findByName(warehouse.getName()).get().getId();
        //将现有容量进行保存，防止被覆盖修改
        warehouse.setCurrentStorage(warehouseRepository.findById(id).get().getCurrentStorage());
        updateWarehouse(id,warehouse);

        Map<String,Object> response = new HashMap<>();
        response.put("success",true);
        response.put("message","修改提交成功，该仓库状态为："+ warehouseRepository.findById(id).get().toString());
        return  response;
    }

    @Override
    public Map<String, Object> getAllWarehouseList() {//查找所有仓库的所有item在本仓库的当前剩余量
        List<Warehouse> warehouseList = warehouseRepository.findAll();

        List<InfoForGraph> list = new ArrayList<>();//列表大小和仓库数量相同

        for (Warehouse warehouse : warehouseList) {
            InfoForGraph graph = new InfoForGraph();
            graph.setWarehouseName(warehouse.getName());
            //每个仓库所对应的一个物品列表存储列表，列表中每个元素包含：仓库id，物品id
            List<CurrentStorage> storageList = currentStorageRepo.findByWarehouse(warehouse);
            List<CurrentItem> items = new ArrayList<>();

            for (CurrentStorage currentStorage : storageList) {
                CurrentItem item = new CurrentItem();
                item.setItemName(currentStorage.getItem().getName());
                item.setQuentity(currentStorage.getCurrentQuantity());
                items.add(item);
            }
            graph.setCurrentItems(items);
            list.add(graph);
        }

        Map<String,Object> response = new HashMap<>();
        response.put("success",true);
        response.put("message","查找成功");
        response.put("warehouseList",list);
        return response;
    }

    @Override
    public Warehouse saveWarehouse(Warehouse warehouse) {
        return warehouseRepository.save(warehouse);
    }

    @Override
    public Optional<Warehouse> findByName(String name) {
        return warehouseRepository.findByName(name);
    }

    @Override
    public Shelf selectShelf(Warehouse warehouse, Double itemWeight) {
        return null;
    }
}

